
Ext.onReady(function(){
    var zjly_from = ""; //始发地
    var zjly_to = ""; //目的地
    var zjly_date = ""; //起飞日期
    var airportTpl = new Ext.XTemplate('<tpl for="." width=220><div class="search-item">', '{airport_name}({airport_code})', '</div></tpl>');
    var airportStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "/admin/getAllAirportComboBox"
        }),
        reader: new Ext.data.JsonReader({
            root: 'root'
        }, [{
            name: 'id'
        }, {
            name: 'airport_name'
        }, {
            name: 'airport_code'
        }])
    });
    
    var zjlyFromAirport = new Ext.form.ComboBox({
        store: airportStore,
        fieldLabel: '始发地',
        name: 'zjly_from',
        allowBlank: true,
        blankText: "始发地必须选择",
        emptyText: '请选择...',
        //typeAhead: false,
        loadingText: 'Searching...',
        //width: 180,
        anchor: '90%',
        //pageSize: 10,
        //hideTrigger: false,
        tpl: airportTpl,
        minChars: 0,
        itemSelector: 'div.search-item',
        onSelect: function(record){
            zjlyFromAirport.setValue(record.data.airport_name);
            zjly_from = record.data.id;
            if (this.isExpanded()) {
                this.collapse();
            }
        }
    });
    var zjlyToAirport = new Ext.form.ComboBox({
        store: airportStore,
        fieldLabel: '目的地',
        name: 'zjly_to',
        allowBlank: true,
        blankText: "目的地必须选择",
        emptyText: '请选择...',
        //typeAhead: false,
        loadingText: 'Searching...',
        //width: 180,
        anchor: '90%',
        //pageSize: 10,
        //hideTrigger: false,
        tpl: airportTpl,
        minChars: 0,
        itemSelector: 'div.search-item',
        onSelect: function(record){
            zjlyToAirport.setValue(record.data.airport_name);
            zjly_to = record.data.id;
            if (this.isExpanded()) {
                this.collapse();
            }
        }
    });
    var zjlyDate = new Ext.form.DateField({
        fieldLabel: '日期',
        name: 'zjly_date',
        altFormats: 'Y-m-d',
        format: 'Y-m-d', //格式化日期
        //disabledDays: [0,6],	//不让选择周六，周日
        //disabledDaysText: "周末要休息",
        invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
        allowBlank: false,
        //emptyText: '请选择...',
        inputType: '2008-01-01',
        //anchor: '50%',
        width: 90,
        value: today
    });
    var zjlyAll = new Ext.form.Radio({
        fieldLabel: '所有',
        hideLabel: true,
        name: 'trip_type',
        boxLabel: '所有',
        checked: true,
        inputValue: ''
    });
    var zjlyLC = new Ext.form.Radio({
        fieldLabel: '联程',
        hideLabel: true,
        name: 'trip_type',
        boxLabel: '联程',
        checked: false,
        inputValue: 'lc'
    });
    var zjlyWF = new Ext.form.Radio({
        fieldLabel: '往返',
        hideLabel: true,
        name: 'trip_type',
        boxLabel: '往返',
        checked: false,
        inputValue: 'wf'
    });
    var zjlyWFLC = new Ext.form.Radio({
        fieldLabel: '联程往返',
        hideLabel: true,
        name: 'trip_type',
        boxLabel: '联程往返',
        checked: false,
        inputValue: 'wflc'
    });

    //提交按钮
    var submitButton = new Ext.Button({
        text: '查询',
        handler: function(){
            if (formPanel.form.isValid()) {
                zjly_date = zjlyDate.getValue().format('Y-m-d');
                zjlyDS.baseParams.type = "";
                if (zjlyLC.checked){
                    zjlyDS.baseParams.type = "lc";
                }else if (zjlyWFLC.checked) {
                    zjlyDS.baseParams.type = "wflc";
                }else if (zjlyWF.checked){
                    zjlyDS.baseParams.type = "wf";
                }
                if (zjlyFromAirport.getValue()==""){
                    zjly_from="";
                }
                if (zjlyToAirport.getValue()==""){
                    zjly_to="";
                }
                zjlyDS.baseParams.from = zjly_from;
                zjlyDS.baseParams.to = zjly_to;
                zjlyDS.baseParams.date = zjly_date;
                zjlyDS.reload();
            }
        }
    })
    
    var formPanel = new Ext.form.FormPanel({
        //baseCls: 'x-plain',
        labelWidth: 55,
        width: '100%',
        frame: true,
        labelAlign: 'right',
        items: [{
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .25,
                layout: 'form',
                border: false,
                items: [zjlyFromAirport]
            }, {
                columnWidth: .25,
                layout: 'form',
                border: false,
                items: [zjlyToAirport]
            }, {
                columnWidth: .15,
                labelWidth: 30,
                layout: 'form',
                border: false,
                items: [zjlyDate]
            }, {
                columnWidth: .05,
                labelWidth: 1,
                layout: 'form',
                border: false,
                items: [zjlyAll]
            }, {
                columnWidth: .05,
                labelWidth: 1,
                layout: 'form',
                border: false,
                items: [zjlyLC]
            }, {
                columnWidth: .05,
                labelWidth: 1,
                layout: 'form',
                border: false,
                items: [zjlyWF]
            }, {
                columnWidth: .1,
                labelWidth: 1,
                layout: 'form',
                border: false,
                items: [zjlyWFLC]
            }, {
                columnWidth: .05,
                layout: 'form',
                border: false,
                items: [submitButton]
            }]
        }]
    });
    
    formPanel.render('zjly_air_form');
    
    //******************转机联运查询结果 ZjlyAirlaneGrid**********************
    var zjlyCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        width: 50,
        hidden: true
    }, {
        header: '始发地',
        dataIndex: 'from',
        width: 100
    }, {
        header: '第一中转地',
        dataIndex: 'mid',
        width: 100
    }, {
        header: '第二中转地',
        dataIndex: 'mid2',
        width: 100
    }, {
        header: '目的地',
        dataIndex: 'to',
        width: 100
    }, {
        header: '第三中转地',
        dataIndex: 'mid3',
        width: 100
    }, {
        header: '第四中转地',
        dataIndex: 'mid4',
        width: 100
    }, {
        header: '终到地',
        dataIndex: 'end',
        width: 100
    }, {
        header: '航空公司',
        dataIndex: 'airways',
        width: 60
    }, {
        header: '生效日期',
        dataIndex: 'start_date',
        width: 70
    }, {
        header: '失效日期',
        dataIndex: 'end_date',
        width: 70
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 50
    }, {
        header: '舱位2',
        dataIndex: 'position2',
        width: 50
    }, {
        header: '舱位3',
        dataIndex: 'position3',
        width: 50
    }, {
        header: '舱位4',
        dataIndex: 'position4',
        width: 50
    }, {
        header: '价格',
        dataIndex: 'price',
        width: 50
    }, {
        header: '代理费',
        dataIndex: 'rake_off',
        width: 50
    }, {
        header: '类型',
        dataIndex: 'trip_type',
        width: 60
    }, {
        header: '政策',
        dataIndex: 'policy',
        width: 150
    }]);
    zjlyCM.defaultSortable = true;
    var zjlyDS = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "/index/searchZjly"
        }),
        reader: new Ext.data.JsonReader({
            root: 'root'
        }, [{
            name: 'id'
        }, {
            name: 'from_id'
        }, {
            name: 'mid_id'
        }, {
            name: 'mid_id2'
        },{
            name: 'to_id'
        }, {
            name: 'mid_id3'
        }, {
            name: 'mid_id4'
        }, {
            name: 'end_id'
        }, {
            name: 'from',
            mapping: 'from_airport.airport_name'
        }, {
            name: 'mid',
            mapping: 'mid_airport.airport_name'
        }, {
            name: 'mid2',
            mapping: 'mid_airport2.airport_name'
        }, {
            name: 'to',
            mapping: 'to_airport.airport_name'
        }, {
            name: 'mid3',
            mapping: 'mid_airport3.airport_name'
        }, {
            name: 'mid4',
            mapping: 'mid_airport4.airport_name'
        }, {
            name: 'end',
            mapping: 'end_airport.airport_name'
        }, {
            name: 'airways_id'
        }, {
            name: 'airways',
            mapping: 'airways.airways_name'
        }, {
            name: 'start_date'
        }, {
            name: 'end_date'
        }, {
            name: 'position'
        }, {
            name: 'position2'
        }, {
            name: 'position3'
        }, {
            name: 'position4'
        }, {
            name: 'price'
        }, {
            name: 'trip_type'
        }, {
            name: 'rake_off'
        }, {
            name: 'is_closed'
        }, {
            name: 'policy'
        }])
    });
    
    var ZjlyAirlaneGridpanel = new Ext.grid.GridPanel({
        el: 'ZjlyAirlaneGrid',
        ds: zjlyDS,
        cm: zjlyCM,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 300,
        //frame: true,
        title: '转机联运特价信息'
    });
    
    ZjlyAirlaneGridpanel.render();
    //zjlyDS.load();
    
    var zjlySrow;
    //############ 票-右键菜单 ##########
    function zjlyRightMenu(){
        var f_id = zjlySrow.get("from_id");
        var m_id = zjlySrow.get("mid_id");
        var m_id2 = zjlySrow.get("mid_id2");
        var t_id = zjlySrow.get("to_id");
        var m_id3 = zjlySrow.get("mid_id3");
        var m_id4 = zjlySrow.get("mid_id4");
        var n_id = zjlySrow.get("end_id");
        var a_id = zjlySrow.get("airways_id");
        var pos = zjlySrow.get("position");
        var pos2 = zjlySrow.get("position2");
        var pos3 = zjlySrow.get("position3");
        var pos4 = zjlySrow.get("position4");
        var t_type = zjlySrow.get("trip_type");
        //alert(f_id+"-"+m_id+"-"+m_id2+"-"+t_id+"-"+m_id3+"-"+m_id4+"-"+n_id+"-"+a_id+"-"+pos+"-"+pos2+"-"+pos3+"-"+pos4+"-"+t_type+"-"+phone_number);
        if (t_type=="往返"){
            t_type="wf";
        }else if (t_type=="联程往返"){
            t_type="wflc";
        }else if (t_type=="联程"){
            t_type="lc";
        }else{
            t_type="";
        }
        //if (t_type=="wflc"){
        //    Ext.Msg.alert('提示', '联程往返类型必须是两个行段对应');
        //}else{
        var tabs = Ext.getCmp('tabs');
        var url = '/index/checkAir2?phone_number='+phone_number+'&from_id='+f_id+
        '&mid_id='+m_id+'&mid_id2='+m_id2+'&to_id='+t_id+'&airways_id='+a_id+
        '&position='+pos+'&position2='+pos2+'&trip_type='+t_type;
        var check_air2_tab = Ext.getCmp('checkAir2_tab');
        if(check_air2_tab){
            check_air2_tab.setTitle('按政策查票');
            check_air2_tab.getUpdater().update(url);
            tabs.activate(check_air2_tab);
        }else{
            tabs.add({
                id: 'checkAir2_tab',
                title: '按政策查票',
                //disabled: true,
                autoLoad: {
                    url: url,
                    scripts: true
                },
                closable: true
            }).show();
        }
    /*
            var check_air_tab = Ext.getCmp('phoneshow_tab1');
            if (check_air_tab) {
                if(check_air_clicked){
                    check_air_tab.getUpdater().update(url);
                }else{
                    check_air_tab.autoLoad.url=url;
                }
                tabs.activate(check_air_tab);
            }
            */
    //}
    }
    var zjlyRightClick = new Ext.menu.Menu({
        items: [{
            handler: zjlyRightMenu,
            text: '【预订】'
        }]
    });
    ZjlyAirlaneGridpanel.on('rowcontextmenu', function(ZjlyAirlaneGridpanel, rowIndex, e){
        var current_selected = zjlyDS.data.items[rowIndex].id;
        zjlySrow = zjlyDS.getById(current_selected);
        e.preventDefault();
        zjlyRightClick.showAt(e.getXY());
    });
})
